
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 初始化浏览器驱动
driver = webdriver.Chrome()
element_list=[]
# 打开目标网页
driver.maximize_window()
driver.get('http://meps-k.com/')
wait=WebDriverWait(driver,5)
# 关闭注册弹窗
element_path='.iconfont.icon-shouyedanchuang-guanbi'
element=wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,element_path)))
element.click()
# 关闭优惠券弹窗
element_path='.iconfont.icon-danchuang-guanbi'
element=wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,element_path)))
element.click()
# 获取页面HTML内容
# content = driver.page_source
#获取首页商品名称
goods_name_element='.text-\[14px\].overflow-hidden'
time.sleep(2)
elements=driver.find_elements(By.CSS_SELECTOR,goods_name_element)
for i in elements:
    element_list.append(i.text)
# 定义您想要滚动到的X和Y坐标
x_coordinate = 0  # 通常是0，除非您想要水平滚动
y_coordinate = 1200 # 这是垂直滚动的距离，您可以根据需要调整
driver.execute_script("window.scrollTo({}, {});".format(x_coordinate, y_coordinate))
time.sleep(1)
# 获取类目里面的商品名称
for i in range(6):
    element_path='.iconfont.text-\[16px\].cursor-pointer.icon-shouyeyouhua-youqiehuan'
    element1=wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,element_path)))
    element1.click()
    time.sleep(1)
    path='//*[@id="widget1699058392W4V3cy"]/div/div/div[2]/div/div/div[{}]/div/a/div[2]/div[1]'.format(i+5)
    element=wait.until(EC.visibility_of_element_located((By.XPATH,path)))
    element_list.append(element.text)
element_list=[i for i in element_list if i ]
print(element_list)
# 关闭浏览器驱动
driver.quit()